package com.mingdao.lhy.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.mingdao.lhy.biz.WishFillingBiz;
import com.mingdao.lhy.constant.Constant;
import com.mingdao.lhy.pojo.MiddleSchoolWish;
import com.mingdao.lhy.pojo.SignUpStudent;
import com.mingdao.lhy.pojo.WishAll;
import com.mingdao.lhy.pojo.WishFive;
import com.mingdao.lhy.pojo.WishFour;
import com.mingdao.lhy.pojo.WishSix;
import com.mingdao.lhy.service.WishFillingService;

@Controller
@RequestMapping(value = "wishFourFiveSixSignUp")
public class WishFourFiveSixController {

	private static Logger log = LoggerFactory.getLogger(WishFourFiveSixController.class);

	/**
	 * #中职志愿填报起止时间
	 */
	@Value("${zzzytbkssj}")
	private String zzzytbkssj;
	
	@Value("${zzzytbjssj}")
	private String zzzytbjssj;
	
	@Autowired
	private WishFillingService wishFillingService;
	@Autowired
	private WishFillingBiz wishFillingBiz;

	@RequestMapping("")
	public String index(Model model, String bmh) {

		HttpSession session = wishFillingBiz.getSession();

		ModelAndView mav = new ModelAndView();
		// HttpSession session = wishFillingBiz.getSession();
		if (null == session || null == session.getAttribute("student")) {
			// session = null;
			log.error("用户未登录，请重新登录");
			return "redirect:/";
		}
		SignUpStudent student = (SignUpStudent) session.getAttribute("student");
		bmh = student.getBmh();
		if (null == bmh || bmh.isEmpty()) {
			model.addAttribute("msg", "没接收到学生信息");
			mav.setViewName("/");
			return "/";
			// return mav;
		}
		/**
		 * 根据传入学生信息先判断是否已经报名，已经报名就显示已报名的信息供修改和确认
		 * 
		 */
		MiddleSchoolWish middleSchoolWish = wishFillingService.getFourFiveSixStudentByBmh(bmh);
		if (null == middleSchoolWish) {
			log.debug("学生还没有填报过中职院校志愿");
			middleSchoolWish = new MiddleSchoolWish();
		}
		model.addAttribute("middleSchoolWish", middleSchoolWish);

		/**
		 * 根据学生报名表获取学生对应的第4，第5，第6批次可填报学校名单
		 */
		// SignUpStudent student = wishFillingService.getStudentByBmh(bmh);
		model.addAttribute("student", student);
		/**
		 * 报名时间判断 6月25日08:00--7月4日23:59之间可用 
		 * 判断是否是州外考生，如果是则返回引导页面
		 */
		Date day = new Date();

		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date now = df.parse(df.format(day)); // 当前时间
			Date startTime = df.parse(zzzytbkssj);
			Date endTime = df.parse(zzzytbjssj); // 截止时间
			if (now.before(endTime) && now.after(startTime)) {
				// 继续正常处理
				System.out.println("继续正常处理");

			} else {
				model.addAttribute("message", "中职志愿非填报时间");
				return "/transform";
			}
		} catch (Exception exception) {
			exception.printStackTrace();
			return "/transform";
		}
		
		
		
		
		
		/**
		 * 根据学生户籍地判断考生是属于11个深度贫困县学生，是则可报第4批次学校，否则不能报
		 */
		String hjd = student.getHjd().substring(0, 6);
		WishFour wishFour = null;
		if (null == Constant.pinkunxian_11.get(hjd) || Constant.pinkunxian_11.get(hjd).isEmpty()) {
			wishFour = new WishFour();
		} else {
			wishFour = wishFillingService.getWishFourSchool(student);
		}
		WishFive wishFive = wishFillingService.getWishFiveSchool(student);
		WishSix wishSix = wishFillingService.getWishSixSchool(student);

		WishAll wishAll = null;
		if (null == session.getAttribute("wishAll")) {
			wishAll = new WishAll();
		} else {
			wishAll = (WishAll) session.getAttribute("wishAll");
		}

		wishAll.setWishFour(wishFour);
		wishAll.setWishFive(wishFive);
		wishAll.setWishSix(wishSix);
		model.addAttribute("wishAll", wishAll);

		mav.setViewName("wishFillingStudent_second");
		mav.addObject("wishAll", wishAll);
		session.setAttribute("wishAll", wishAll);
		return "wishFillingStudent_second";
		// return mav;
	}

}
